home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / modes / view.el < prev    next >
Encoding:
Text File  |  1995-06-08  |  14.6 KB  |  377 lines

  1. ;;; view.el --- peruse file or buffer without editing.
  2. ;; Keywords: wp unix
  3.  
  4. ;; Copyright (C) 1985-1993 Free Software Foundation, Inc.
  5. ;; Principal author K. Shane Hartman
  6. ;; Copyright (C) 1994 Jonathan Stigelman <Stig@hackvan.com>
  7.  
  8. ;; This file is part of XEmacs.
  9.  
  10. ;; XEmacs is free software; you can redistribute it and/or modify it
  11. ;; under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; XEmacs is distributed in the hope that it will be useful, but
  16. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18. ;; General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  22. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. (require 'helper)
  25.  
  26. (defvar view-mode-map nil)
  27. (if view-mode-map
  28.     nil
  29.   (setq view-mode-map (make-keymap))
  30.   (set-keymap-name view-mode-map 'view-mode-map)
  31.   (suppress-keymap view-mode-map)
  32.   (define-key view-mode-map "\C-c" 'view-exit)
  33.   (define-key view-mode-map "\C-z" 'suspend-emacs)
  34.   (define-key view-mode-map "q" 'view-exit)
  35.   (define-key view-mode-map "<" 'beginning-of-buffer)
  36.   (define-key view-mode-map ">" 'end-of-buffer)
  37.   (define-key view-mode-map "\M-v" 'View-scroll-lines-backward)
  38.   (define-key view-mode-map "\C-v" 'View-scroll-lines-forward)
  39.   (define-key view-mode-map " " 'View-scroll-lines-forward)
  40.   (define-key view-mode-map "\177" 'View-scroll-lines-backward)
  41.   (define-key view-mode-map 'backspace 'View-scroll-lines-backward)
  42.   (define-key view-mode-map "\n" 'View-scroll-one-more-line)
  43.   (define-key view-mode-map "\r" 'View-scroll-one-more-line)
  44.   (define-key view-mode-map "z" 'View-scroll-lines-forward-set-scroll-size)
  45.   (define-key view-mode-map "g" 'View-goto-line)
  46.   (define-key view-mode-map "=" 'what-line)
  47.   (define-key view-mode-map "." 'set-mark-command)
  48.   (define-key view-mode-map "'" 'View-back-to-mark)
  49.   (define-key view-mode-map "@" 'View-back-to-mark)  
  50.   (define-key view-mode-map "x" 'exchange-point-and-mark)
  51.   (define-key view-mode-map "h" 'Helper-describe-bindings)
  52.   (define-key view-mode-map "?" 'Helper-describe-bindings)
  53.   ;; (define-key view-mode-map "\C-h" 'Helper-help)
  54.   (define-key view-mode-map '(control h) 'Helper-help)
  55.   (define-key view-mode-map "s" 'isearch-forward)
  56.   (define-key view-mode-map "r" 'isearch-backward)
  57.   (define-key view-mode-map "/" 'View-search-regexp-forward)
  58.   (define-key view-mode-map "\\" 'View-search-regexp-backward)
  59.   ;; This conflicts with the standard binding of isearch-regexp-forward
  60.   (define-key view-mode-map "\M-\C-s" 'View-search-regexp-forward)
  61.   (define-key view-mode-map "\M-\C-r" 'View-search-regexp-backward)  
  62.   (define-key view-mode-map "n" 'View-search-last-regexp-forward)
  63.   (define-key view-mode-map "p" 'View-search-last-regexp-backward)
  64.   )
  65.  
  66. ;; view-mode buffer-local vars
  67. (defvar view-old-mode-line-buffer-identification)
  68. (defvar view-old-buffer-read-only)
  69. (defvar view-old-mode-name)
  70. (defvar view-old-major-mode)
  71. (defvar view-old-local-map)
  72. (defvar view-old-Helper-return-blurb)
  73. (defvar view-exit-action)
  74. (defvar view-prev-buffer)
  75. (defvar view-exit-position)
  76. (defvar view-scroll-size)
  77. (defvar view-last-regexp)
  78.  
  79. (defun view-file (file-name &optional other-window)
  80.   "View FILE in View mode, returning to previous buffer when done.
  81. With a prefix argument, view it in another window.
  82. The usual Emacs commands are not available; instead,
  83. a special set of commands (mostly letters and punctuation)
  84. are defined for moving around in the buffer.
  85. Space scrolls forward, Delete scrolls backward.
  86. For list of all View commands, type ? or h while viewing.
  87.  
  88. Calls the value of  view-hook  if that is non-nil."
  89.   (interactive "fView file: \nP")
  90.   (let ((old-buf (current-buffer))
  91.     (had-a-buf (get-file-buffer file-name))
  92.     (buf-to-view (find-file-noselect file-name)))
  93.     (if other-window
  94.     (pop-to-buffer buf-to-view)
  95.       (switch-to-buffer buf-to-view t))
  96.     (view-mode old-buf
  97.            (and (not had-a-buf)
  98.             (not (buffer-modified-p buf-to-view))
  99.             'kill-buffer))))
  100.  
  101. (defun view-buffer (buffer-name &optional other-window)
  102.   "View BUFFER in View mode, returning to previous buffer when done.
  103. With a prefix argument, view it in another window.
  104. The usual Emacs commands are not available; instead,
  105. a special set of commands (mostly letters and punctuation)
  106. are defined for moving around in the buffer.
  107. Space scrolls forward, Delete scrolls backward.
  108. For list of all View commands, type ? or h while viewing.
  109.  
  110. Calls the value of  view-hook  if that is non-nil."
  111.   (interactive "bView buffer: \nP")
  112.   (let ((old-buf (current-buffer)))
  113.     (if other-window
  114.     (pop-to-buffer buffer-name)
  115.       (switch-to-buffer buffer-name t))
  116.     (view-mode old-buf nil)))
  117.  
  118. (defun view-file-other-window (file)
  119.   "Find FILE in other window, and enter view mode."
  120.   (view-file file t))
  121.  
  122. (defun view-buffer-other-window (buffer)
  123.   "Switch to BUFFER in another window, and enter view mode."
  124.   (view-buffer buffer t))
  125.  
  126. (defun view-mode (&optional prev-buffer action)
  127.   "Major mode for viewing text but not editing it.
  128. Letters do not insert themselves.  Instead these commands are provided.
  129. Most commands take prefix arguments.  Commands dealing with lines
  130. default to \"scroll size\" lines (initially size of window).
  131. Search commands default to a repeat count of one.
  132. M-< or <    move to beginning of buffer.
  133. M-> or >    move to end of buffer.
  134. C-v or Space    scroll forward lines.
  135. M-v or DEL    scroll backward lines.
  136. CR or LF    scroll forward one line (backward with prefix argument).
  137. z        like Space except set number of lines for further
  138.            scrolling commands to scroll by.
  139. C-u and Digits    provide prefix arguments.  `-' denotes negative argument.
  140. =        prints the current line number.
  141. g        goes to line given by prefix argument.
  142. / or M-C-s    searches forward for regular expression
  143. \\ or M-C-r    searches backward for regular expression.
  144. n        searches forward for last regular expression.
  145. p        searches backward for last regular expression.
  146. C-@ or .    set the mark.
  147. x        exchanges point and mark.
  148. C-s or s    do forward incremental search.
  149. C-r or r    do reverse incremental search.
  150. @ or '        return to mark and pops mark ring.
  151.           Mark ring is pushed at start of every
  152.           successful search and when jump to line to occurs.
  153.           The mark is set on jump to buffer start or end.
  154. ? or h        provide help message (list of commands).
  155. C-h        provides help (list of commands or description of a command).
  156. C-n        moves down lines vertically.
  157. C-p        moves upward lines vertically.
  158. C-l        recenters the screen.
  159. q or C-c    exit view-mode and return to previous buffer.
  160.  
  161. Entry to this mode calls the value of  view-hook  if non-nil.
  162. \\{view-mode-map}"
  163.   (interactive)
  164.   (or prev-buffer (setq prev-buffer (current-buffer)))
  165.  
  166.   (set (make-local-variable 'view-old-mode-line-buffer-identification)
  167.        mode-line-buffer-identification)
  168.   (set (make-local-variable 'view-old-buffer-read-only) buffer-read-only)
  169.   (set (make-local-variable 'view-old-mode-name)    mode-name)
  170.   (set (make-local-variable 'view-old-major-mode)    major-mode)
  171.   (set (make-local-variable 'view-old-local-map)    (current-local-map))
  172.   (set (make-local-variable 'view-old-Helper-return-blurb)
  173.        (and (boundp 'Helper-return-blurb) Helper-return-blurb))
  174.  
  175.   (setq buffer-read-only t
  176.     mode-name "View"
  177.     major-mode 'view-mode
  178.     mode-line-buffer-identification (list
  179.                      (if (buffer-file-name)
  180.                          "Viewing %f"
  181.                        "Viewing %b"))
  182.     Helper-return-blurb (format "continue viewing %s"
  183.                     (if (buffer-file-name)
  184.                     (file-name-nondirectory (buffer-file-name))
  185.                       (buffer-name))))
  186.  
  187.   (set (make-local-variable 'view-exit-action)     action)
  188.   (set (make-local-variable 'view-prev-buffer)       prev-buffer)
  189.   (set (make-local-variable 'view-exit-position)   (point-marker))
  190.  
  191.   (set (make-local-variable 'view-scroll-size)       nil)
  192.   (set (make-local-variable 'view-last-regexp)       nil)
  193.  
  194.   (beginning-of-line)
  195.   (setq goal-column nil)
  196.  
  197.   (use-local-map view-mode-map)
  198.   (run-hooks 'view-hook)
  199.   (view-helpful-message))
  200.  
  201. (defun view-exit ()
  202.   "Exit from view-mode.
  203. If you viewed an existing buffer, that buffer returns to its previous mode.
  204. If you viewed a file that was not present in Emacs, its buffer is killed."
  205.   (interactive)
  206.   (setq mode-line-buffer-identification view-old-mode-line-buffer-identification
  207.     major-mode            view-old-major-mode
  208.     mode-name            view-old-mode-name
  209.     buffer-read-only        view-old-buffer-read-only)
  210.   (use-local-map (current-local-map))
  211.  
  212.   (goto-char view-exit-position)
  213.   (set-marker view-exit-position nil)
  214.  
  215.   ;; Now do something to the buffer that we were viewing
  216.   ;; (such as kill it).
  217.   (let ((viewed-buffer (current-buffer))
  218.     (action view-exit-action))
  219.     (switch-to-buffer view-prev-buffer)
  220.     (if action (funcall action viewed-buffer))))
  221.  
  222. (defun view-helpful-message ()
  223.   (message
  224.    (if (and (eq (key-binding "\C-h") 'Helper-help)
  225.         (eq (key-binding "?") 'Helper-describe-bindings)
  226.         (eq (key-binding "\C-c") 'view-exit))
  227.        "Type C-h for help, ? for commands, C-c to quit"
  228.      (substitute-command-keys
  229.       "Type \\[Helper-help] for help, \\[Helper-describe-bindings] for commands, \\[exit-recursive-edit] to quit."))))
  230.  
  231. ;(defun View-undefined ()
  232. ;  (interactive)
  233. ;  (ding)
  234. ;  (view-helpful-message))
  235.  
  236. (defun view-window-size () (1- (window-height)))
  237.  
  238. (defun view-scroll-size ()
  239.   (min (view-window-size) (or view-scroll-size (view-window-size))))
  240.  
  241. (defvar view-hook nil
  242.   "If non-nil, its value is called when viewing buffer or file.")
  243.  
  244. ;(defun view-last-command (&optional who what)
  245. ;  (setq view-last-command-entry this-command)
  246. ;  (setq view-last-command who)
  247. ;  (setq view-last-command-argument what))
  248.  
  249. ;(defun View-repeat-last-command ()
  250. ;  "Repeat last command issued in View mode."
  251. ;  (interactive)
  252. ;  (if (and view-last-command
  253. ;       (eq view-last-command-entry last-command))
  254. ;      (funcall view-last-command view-last-command-argument))
  255. ;  (setq this-command view-last-command-entry))
  256.  
  257. (defun View-goto-line (&optional line)
  258.   "Move to LINE in View mode.
  259. Display is centered at LINE.  Sets mark at starting position and pushes
  260. mark ring."
  261.   (interactive "p")
  262.   (push-mark)
  263.   (goto-line (or line 1))
  264.   (recenter (/ (view-window-size) 2)))
  265.  
  266. (defun View-scroll-lines-forward (&optional lines)
  267.   "Scroll forward in View mode, or exit if end of text is visible.
  268. No arg means whole window full, or number of lines set by \\[View-scroll-lines-forward-set-scroll-size].
  269. Arg is number of lines to scroll."
  270.   (interactive "P")
  271.   (if (pos-visible-in-window-p (point-max))
  272.       (exit-recursive-edit))
  273.   (setq lines
  274.     (if lines (prefix-numeric-value lines)
  275.       (view-scroll-size)))
  276. ; (view-last-command 'View-scroll-lines-forward lines)
  277.   (if (>= lines (view-window-size))
  278.       (scroll-up nil)
  279.     (if (>= (- lines) (view-window-size))
  280.     (scroll-down nil)
  281.       (scroll-up lines)))
  282.   (cond ((pos-visible-in-window-p (point-max))
  283.      (goto-char (point-max))
  284.      (recenter -1)
  285.      (message (substitute-command-keys
  286.         "End.  Type \\[exit-recursive-edit] to quit viewing."))))
  287.   (move-to-window-line -1)
  288.   (beginning-of-line))
  289.  
  290. (defun View-scroll-lines-forward-set-scroll-size (&optional lines)
  291.   "Scroll forward LINES lines in View mode, setting the \"scroll size\".
  292. This is the number of lines which \\[View-scroll-lines-forward] and \\[View-scroll-lines-backward] scroll by default.
  293. The absolute value of LINES is used, so this command can be used to scroll
  294. backwards (but \"scroll size\" is always positive).  If LINES is greater than
  295. window height or omitted, then window height is assumed.  If LINES is less
  296. than window height then scrolling context is provided from previous screen."
  297.   (interactive "P")
  298.   (if (not lines)
  299.       (setq view-scroll-size (view-window-size))
  300.     (setq lines (prefix-numeric-value lines))
  301.     (setq view-scroll-size
  302.       (min (if (> lines 0) lines (- lines)) (view-window-size))))
  303.   (View-scroll-lines-forward lines))
  304.  
  305. (defun View-scroll-one-more-line (&optional arg)
  306.   "Scroll one more line up in View mode.
  307. With ARG scroll one line down."
  308.   (interactive "P")
  309.   (View-scroll-lines-forward (if (not arg) 1 -1)))
  310.  
  311. (defun View-scroll-lines-backward (&optional lines)
  312.   "Scroll backward in View mode.
  313. No arg means whole window full, or number of lines set by \\[View-scroll-lines-forward-set-scroll-size].
  314. Arg is number of lines to scroll."
  315.   (interactive "P")
  316.   (View-scroll-lines-forward (if lines
  317.                  (- (prefix-numeric-value lines))
  318.                    (- (view-scroll-size)))))
  319.   
  320. (defun View-search-regexp-forward (times regexp)
  321.   "Search forward for NTH occurrence of REGEXP in View mode.
  322. Displays line found at center of window.  REGEXP is remembered for
  323. searching with \\[View-search-last-regexp-forward] and \\[View-search-last-regexp-backward].  Sets mark at starting position and pushes mark ring."
  324.   (interactive "p\nsSearch forward (regexp): ")
  325.   (if (> (length regexp) 0)
  326.       (progn
  327.        ;(view-last-command 'View-search-last-regexp-forward times)
  328.     (view-search times regexp))))
  329.  
  330. (defun View-search-regexp-backward (times regexp)
  331.   "Search backward from window start for NTH instance of REGEXP in View mode.
  332. Displays line found at center of window.  REGEXP is remembered for
  333. searching with \\[View-search-last-regexp-forward] and \\[View-search-last-regexp-backward].  Sets mark at starting position and pushes mark ring."
  334.   (interactive "p\nsSearch backward (regexp): ")
  335.   (View-search-regexp-forward (- times) regexp))
  336.  
  337. (defun View-search-last-regexp-forward (times)
  338.   "Search forward from window end for NTH instance of last regexp in View mode.
  339. Displays line found at center of window.  Sets mark at starting position
  340. and pushes mark ring."
  341.   (interactive "p")
  342.   (View-search-regexp-forward times view-last-regexp))
  343.  
  344. (defun View-search-last-regexp-backward (times)
  345.   "Search backward from window start for NTH instance of last regexp in View mode.
  346. Displays line found at center of window.  Sets mark at starting position and
  347. pushes mark ring."
  348.   (interactive "p")
  349.   (View-search-regexp-backward times view-last-regexp))
  350.  
  351. (defun View-back-to-mark (&optional ignore)
  352.   "Return to last mark set in View mode, else beginning of file.
  353. Displays line at center of window.  Pops mark ring so successive
  354. invocations return to earlier marks."
  355.   (interactive)
  356.   (goto-char (or (mark) (point-min)))
  357.   (pop-mark)
  358.   (recenter (/ (view-window-size) 2)))
  359.          
  360. (defun view-search (times regexp)
  361.   (setq view-last-regexp regexp)
  362.   (let (where)
  363.     (save-excursion
  364.       (move-to-window-line (if (< times 0) 0 -1))
  365.       (if (re-search-forward regexp nil t times)
  366.       (setq where (point))))
  367.     (if where
  368.     (progn
  369.       (push-mark)
  370.       (goto-char where)
  371.       (beginning-of-line)
  372.       (recenter (/ (view-window-size) 2)))
  373.       (message "Can't find occurrence %d of %s" times regexp)
  374.       (sit-for 4))))
  375.  
  376. (provide 'view)
  377.